package Json;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

/**
 * @author macpro
 * @date 2019-08-06
 * @function  通用解析
 */
public class GsonParseTComonResponse {
    public static void main(String[] args) {

        String modelStr = "{\n" +
                "          \"address\": \"蜂盒科技FC242218480001\",\n" +
                "          \"isBinding\": true\n" +
                "       }";
        String str = " {\n" +
                "       \"success\": true,\n" +
                "       \"errorMsg\": \"\",\n" +
                "       \"errorCode\": \"\",\n" +
                "       \"model\": {\n" +
                "          \"address\": \"蜂盒科技FC242218480001\",\n" +
                "          \"isBinding\": true\n" +
                "       },\n" +
                "       \"attributes\": null\n" +
                "    }";

        String strQr = "{\n" +
                "       \"success\": true,\n" +
                "       \"errorMsg\": \"\",\n" +
                "       \"errorCode\": \"\",\n" +
                "       \"model\": [{ \"visitorName\": \"何二狗\", \"attendeeCompany\": \"众安科技\", \"visitStartTime\": \"2019-07-12 15:00\", \"username\": \"何劭铭\"\n" +
                "\n" +
                "},{ \"visitorName\": \"何二狗\", \"attendeeCompany\": \"众安科技\", \"visitStartTime\": \"2019-07-12 15:00\", \"username\": \"何劭铭\"\n" +
                "\n" +
                "}],\n" +
                "       \"attributes\": null\n" +
                "    }";
        CommonResponse<BindCheckResponse> bean = new Gson().fromJson(str, new TypeToken<CommonResponse<BindCheckResponse>>() {
        }.getType());

        CommonResponse commonResponse = new Gson().fromJson(str, CommonResponse.class);
        System.out.println("json only common "+commonResponse.isSuccess());
        if (bean != null) {
            if (bean.isSuccess() && bean.getModel()!=null) {
                if (bean.getModel().isIsBiding()) {
                    System.out.println("success");

                } else {
                    System.out.println("false");

                }
            }
        }

        BindCheckResponse bindCheckResponse = new Gson().fromJson(modelStr, BindCheckResponse.class);
        System.out.println("parse model only "+bindCheckResponse.isIsBiding());


        CommonResponse<List<QrCodeResponse>> beanQr = new Gson().fromJson(strQr, new TypeToken<CommonResponse<List<QrCodeResponse>>>() {}.getType());

        System.out.println(beanQr.getModel().get(0).getUsername());

//        CommonResponse<UserRes> bean = new CommonResponse<>();
//        bean.setSuccess(true);
//        UserRes userRes = new UserRes();
//        bean.setModel(userRes);
//        userRes.setAge(30);
//        userRes.name="A san ";
//        CommonResponse<UserRes> res = getRes(bean, new Gson().toJson(bean));
//        System.out.println(" res "+res.isSuccess());
//        System.out.println(" res "+res.isSuccess()+"  "+res.getModel().name);
    }

    public static <T> CommonResponse<T> getRes(CommonResponse<T> commonResponse,String str){
        System.out.println(commonResponse.getClass()+" "+commonResponse.getModel().getClass());
        // TODO: 2019-08-06 泛型消失
        Object o = new Gson().fromJson(str, new TypeToken<CommonResponse<T>>() {
        }.getType());
        return (CommonResponse<T>)o;
    }



}
class UserRes{
    public String name;
    public int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}


class BindCheckResponse {

    /**
     * isBiding : true
     * address : 上海-1 号楼
     */

    private boolean isBiding;
    private String address;

    public boolean isIsBiding() {
        return isBiding;
    }

    public void setIsBiding(boolean isBiding) {
        this.isBiding = isBiding;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}


class CommonResponse<T> {

    /**
     * success : false
     * errorMsg : 请求失败，请重试
     * errorCode :
     * model :
     * attributes :
     */

    public boolean success;
    public String errorMsg;
    public String errorCode;
    public T model;
    public String attributes;

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public T getModel() {
        return model;
    }

    public void setModel(T model) {
        this.model = model;
    }

    public String getAttributes() {
        return attributes;
    }

    public void setAttributes(String attributes) {
        this.attributes = attributes;
    }
}

class QrCodeResponse {

    /**
     * visitorName : 何二狗
     * attendeeCompany : 众安科技
     * visitStartTime : 2019-07-12 15:00
     * username : 何劭铭
     */

    private String visitorName;
    private String attendeeCompany;
    private String visitStartTime;
    private String username;

    public String getVisitorName() {
        return visitorName;
    }

    public void setVisitorName(String visitorName) {
        this.visitorName = visitorName;
    }

    public String getAttendeeCompany() {
        return attendeeCompany;
    }

    public void setAttendeeCompany(String attendeeCompany) {
        this.attendeeCompany = attendeeCompany;
    }

    public String getVisitStartTime() {
        return visitStartTime;
    }

    public void setVisitStartTime(String visitStartTime) {
        this.visitStartTime = visitStartTime;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}
